package org.jeecgframework.web.util;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

/**
 * @author xiul
 * 2016-7-21 上午11:20:02
 * Description 
 */
public class GetWSByAxis2 {
	private static String NameSpace="http://service.bk.cn";
	private static String urn = "urn:cbsIpTask";
	private QName opAddEntry; 
	private String WSUrl;
	private String[] args;
	
	public GetWSByAxis2(){}
	
	public GetWSByAxis2(String WSUrl,String[] args){
		this.WSUrl = WSUrl;
		this.args = args;
	}
	
	private RPCServiceClient setOption() throws AxisFault {
		RPCServiceClient serviceClient = new RPCServiceClient();
		Options options = serviceClient.getOptions();
		EndpointReference targetEPR = new EndpointReference(WSUrl);
		options.setAction(urn);
		options.setTo(targetEPR);
		return serviceClient;
	}
	private QName getQname(){
		return new QName (NameSpace,"getName");
	}
	//返回String
	public String getReturnStr() throws AxisFault {
		RPCServiceClient serviceClient = this.setOption();
		opAddEntry = this.getQname();
		String str = (String) serviceClient.invokeBlocking(opAddEntry, args, new Class[]{String.class })[0];
		return str;
	}
	// 返回一维String数组
	public String[] getArray() throws AxisFault{
		RPCServiceClient serviceClient =this.setOption();
		opAddEntry = this.getQname();
		String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry, args, new Class[]{String[].class })[0];
		return strArray;
	}
	//从WebService中返回一个对象的实例
	public Object getObject(Object o) throws AxisFault{ 
		RPCServiceClient serviceClient = this.setOption(); 
		opAddEntry = this.getQname();
		Object object = serviceClient.invokeBlocking(opAddEntry, args,new Class[]{o.getClass()})[0];
		return object;
	}
}
